package com.naire.modul.projecperiods.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.common.dao.GeneralDaoImpl;
import net.common.service.PageBean;
import net.common.util.BeanUtils;

import com.naire.modul.projecperiods.dao.ProjecperiodsDao;
import com.naire.modul.projecperiods.pojo.Projecperiods;
import com.naire.modul.projecperiods.vo.ProjecperiodsVo;
import com.naire.modul.project.pojo.Project;

public class ProjecperiodsDaoImpl extends GeneralDaoImpl implements
		ProjecperiodsDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<ProjecperiodsVo> search(PageBean pageBean, Integer project_id)
			throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("project_id", project_id);
		StringBuilder hql = new StringBuilder(
				"from Projecperiods where project.project_id=:project_id");
		List<ProjecperiodsVo> d = new ArrayList<ProjecperiodsVo>();
		List<Projecperiods> l = hibernateHandler.query(hql, pageBean, params);
		for (Projecperiods bean : l) {
			ProjecperiodsVo dv = new ProjecperiodsVo();
			BeanUtils.copyProperties(bean, dv);
			d.add(dv);
		}
		return d;
	}

	@Override
	public void insertList(List<ProjecperiodsVo> list, Integer project_id)
			throws Exception {
		Project p = (Project) hibernateHandler.find(Project.class, project_id);
		int i = 0;
		for (ProjecperiodsVo v : list) {
			Projecperiods pojo = new Projecperiods();
			BeanUtils.copyProperties(v, pojo);
			pojo.setProject(p);
			pojo.setSeq_num(++i);
			hibernateHandler.insert(pojo);
		}
	}

	@Override
	public void insert(ProjecperiodsVo bean) throws Exception {
		Project p = (Project) hibernateHandler.find(Project.class, bean.getProject_id());
		Projecperiods pojo = new Projecperiods();
		BeanUtils.copyProperties(bean, pojo);
		pojo.setProject(p);
		hibernateHandler.insert(pojo);
	}

	@Override
	public void update(ProjecperiodsVo bean) throws Exception {
		Projecperiods pojo = (Projecperiods) hibernateHandler.find(
				Projecperiods.class, bean.getPeriods_id());
		BeanUtils.copyProperties(bean, pojo);
		hibernateHandler.update(pojo);
	}

	@Override
	public void updateFlag(ProjecperiodsVo bean) throws Exception {
		update(bean);
	}

	@Override
	public ProjecperiodsVo find(Integer id) throws Exception {
		Projecperiods pojo = (Projecperiods) hibernateHandler.find(
				Projecperiods.class, id);
		ProjecperiodsVo bean = new ProjecperiodsVo();
		BeanUtils.copyProperties(pojo, bean);
		return bean;
	}

	@Override
	public void delete(Integer id) throws Exception {
		Projecperiods pojo = (Projecperiods) hibernateHandler.find(
				Projecperiods.class, id);
		hibernateHandler.delete(pojo);
	}
}
